package com.chapter.sample.juc;

import java.util.concurrent.*;

/**
 * API
 */
public class CompletableFutureAPIDemo {
    static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            8,
            16,
            10,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());


    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {


    }

    private static void m1() {
        //获得结果和触发计算
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 1;
        }, executor);

        //System.out.println(future.get());
        //System.out.println(future.get(1L, TimeUnit.SECONDS));
        //System.out.println(future.join());

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(future.complete(-44));
    }
}
